<template>
  <div>
    <div>
      <el-input size="small" v-model="ma.name" style="width: 300px" prefix-icon="el-icon-plus"
                placeholder="请在这里输入需要添加的专业的名称..."></el-input>
      <el-select v-model="ma.parentId"
                 palaceholder="选择学院名称"
                 size="small"
                 style="margin-left: 6px;margin-right: 6px"
                 @change="departChange"
                 option-filter-prop="children"
                 label-in-value
                 >
        <el-option v-for="item in departments"
                   :key="item"
                   :label="item"
                   :value="item">
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-plus" size="small" @click="addMajor">添加</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table :data="majors"
                border
                stripe
                size="small"
                style="width: 80%">
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="专业名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="parentId"
            label="学院名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间"
            width="150">
        </el-table-column>
        <el-table-column prop="enabled"
                         label="是否启用"
                         width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="small" @click="showEditView(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteHandler(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
        title="编辑专业信息"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>专业名称</el-tag>
            </td>
            <td>
              <el-input v-model="updateMajor.name" size="small"></el-input>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>学院名称</el-tag>
            </td>
            <td>
              <el-select v-model="updateMajor.parentId"
                         placeholder="学院名称" size="small"
                         @change="departChange"
                         option-filter-prop="children"
                         label-in-value
                         >
                <el-option
                    v-for="item in departments"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>是否启用</el-tag>
            </td>
            <td>
              <el-switch
                  v-model="updateMajor.enabled"
                  activecolor="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="禁用">
              </el-switch>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "maSet",
  data() {
    return {
      ma: {
        name: '',
        parentId: ''
      },
      majors: [],
      updateMajor: {
        name: '',
        parentId: '',
        enabled: false
      },
      dialogVisible: false,
      departments: [],
    }
  },
  created() {
    this.queryDepartments();
  },
  mounted() {
    this.initMajors();

  },
  methods: {
    departChange(value){
      this.ma.parentId = value;
      this.updateMajor.parentId = value;
    },
    //获取学院列表
    queryDepartments() {
      this.getRequest('/system/basic/depart/').then(resp => {
        if (resp) {
          for (var i = 0; i < resp.length;i++){
            this.departments[i] = resp[i].name;
          }
        }
      })
    },
    //修改职称
    doUpdate() {
      this.putRequest('/system/basic/major/', this.updateMajor).then(resp => {
        if (resp) {
          console.log(this.updateMajor)
          this.initMajors();
          this.updateMajor.name = '';
          this.updateMajor.parentId = '';
          this.dialogVisible = false;
        }
      })
    },
    showEditView(data) {
      Object.assign(this.updateMajor, data);
      this.updateMajor.createDate = '';
      this.dialogVisible = true;
    },
    deleteHandler(data) {
      //删除
      this.$confirm('此操作将永久删除【' + data.name + '】, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/major/' +
            data.id).then(resp => {
          if (resp) {
            this.initMajors();
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addMajor() {
      //添加专业
      if (this.ma.name && this.ma.parentId) {
        this.postRequest('/system/basic/major/', this.ma).then(resp => {
          if (resp) {
            this.initMajors();
            this.ma.name = '';
            this.ma.parentId = '';
          }
        })
      } else {
        this.$message.error("字段不能为空！");
      }

    },
    initMajors() {
      this.getRequest('/system/basic/major/').then(resp => {
        if (resp) {
          this.majors = resp;
        }
      })
    }
  }
}
</script>

<style>

</style>
